<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2024/2/23
 * Time: 11:21
 */

namespace app\api\controller;

use app\api\model\Address AS AddressModel;
use think\Db;

class Address extends Base
{
    public function __construct()
    {
        parent::__construct();

    }

    /**
     * 收货地址列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function addressList(){
        $where = ['addr_user_id' => $this->user_id];

        $data = AddressModel::selectData($where);
        $count = AddressModel::countData($where);
        if($data){
            return json(['status' => 200, 'msg' => $data, 'count' => $count]);
        }else{
            return json(['status' => 200, 'msg' => [], 'count' => 0]);
        }

    }

    /**
     * 创建收货地址
     * addr_xing       姓
     * addr_name       名
     * addr_phone      联系电话
     * addr_postal     邮编
     * addr_company    公司
     * addr_address    详细地址
     * addr_default    是否默认   1 默认   2 非默认
     * @return \think\response\Json
     */
    public function createAddress(){
        $addr_xing = input('addr_xing');
        $addr_name = input('addr_name');
        $addr_phone = input('addr_phone');
        $addr_postal = input('addr_postal');
        $addr_company = input('addr_company');
        $addr_address = input('addr_address');
        $addr_default = input('addr_default');

        $create = [
            'addr_user_id' => $this->user_id,
            'addr_xing' => $addr_xing,
            'addr_name' => $addr_name,
            'addr_phone' => $addr_phone,
            'addr_postal' => $addr_postal,
            'addr_company' => $addr_company,
            'addr_address' => $addr_address,
            'addr_default' => $addr_default,
            'addr_time' => initTime()
        ];

        Db::startTrans();
        try{
            // 如果是默认收货地址，现将现默认收货地址改正非默认
            if($addr_default == 1){
                $hasDefault = AddressModel::findData(['addr_user_id' => $this->user_id, 'addr_default' => 1]);
                if($hasDefault){
                    AddressModel::update(['addr_default' => 2], ['addr_id' => $hasDefault['addr_id']]);
                }
            }

            AddressModel::create($create);

            Db::commit();
            return json(['status' => 200, 'msg' => $this->lanMsg->doSuccess[$this->lanType]]);
        }catch (\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => $e->getMessage()]);
        }
    }

    /**
     * 收货地址详情
     * addr_id   非必传，单条收货地址详情时需要传，某个用户默认收货地址时不用传
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function showAddress(){
        $addr_id = input('addr_id');

        // 有addr_id，查询某条记录，没有就查询某个用户的默认收货地址
        if($addr_id){
            $where = ['addr_id' => $addr_id];
        }else{
            $where = ['addr_user_id' => $this->user_id, 'addr_default' => 1];
        }

        $data = AddressModel::findData($where);
        return json(['status' => 200, 'msg' => $data]);
    }

    /**
     * 编辑收货地址
     * addr_id         收货地址ID
     * addr_xing       姓
     * addr_name       名
     * addr_phone      联系电话
     * addr_postal     邮编
     * addr_company    公司
     * addr_address    详细地址
     * addr_default    是否默认   1 默认   2 非默认
     * @return \think\response\Json
     */
    public function updateAddress(){
        $addr_id = input('addr_id');
        $addr_xing = input('addr_xing');
        $addr_name = input('addr_name');
        $addr_phone = input('addr_phone');
        $addr_postal = input('addr_postal');
        $addr_company = input('addr_company');
        $addr_address = input('addr_address');
        $addr_default = input('addr_default');

        $update = [
            'addr_xing' => $addr_xing,
            'addr_name' => $addr_name,
            'addr_phone' => $addr_phone,
            'addr_postal' => $addr_postal,
            'addr_company' => $addr_company,
            'addr_address' => $addr_address,
            'addr_default' => $addr_default,
            'addr_update_time' => initTime()
        ];

        Db::startTrans();
        try{
            // 如果是默认收货地址，现将现默认收货地址改正非默认
            if($addr_default == 1){
                $hasDefault = AddressModel::findData(['addr_user_id' => $this->user_id, 'addr_default' => 1]);
                if($hasDefault && $hasDefault['addr_id'] != $addr_id){
                    AddressModel::update(['addr_default' => 2], ['addr_id' => $hasDefault['addr_id']]);
                }
            }

            AddressModel::update($update, ['addr_id' => $addr_id]);

            Db::commit();
            return json(['status' => 200, 'msg' => $this->lanMsg->doSuccess[$this->lanType]]);
        }catch (\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => $e->getMessage()]);
        }
    }

    /**
     * 删除收货地址
     * addr_id    收货地址ID
     * @return \think\response\Json
     */
    public function delAddress(){
        $addr_id = input('addr_id');
        $res = AddressModel::update(['addr_del' => 2], ['addr_id' => $addr_id]);
        if($res){
            return json(['status' => 200, 'msg' => $this->lanMsg->doSuccess[$this->lanType]]);
        }else{
            return json(['status' => 200, 'msg' => $this->lanMsg->systemErr[$this->lanType]]);
        }
    }

    /**
     * 设置/取消默认收货地址
     * addr_id    收货地址ID
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function setDefault(){
        $addr_id = input('addr_id');

        $addr = AddressModel::findData(['addr_id' => $addr_id]);

        $default = $addr['addr_default'] == 1 ? 2 : 1;

        Db::startTrans();
        try{
            if($default == 1){
                $hasDefault = AddressModel::findData(['addr_user_id' => $this->user_id, 'addr_default' => 1]);
                if($hasDefault && $hasDefault['addr_id'] != $addr_id){
                    AddressModel::update(['addr_default' => 2], ['addr_id' => $hasDefault['addr_id']]);
                }
            }

            AddressModel::update(['addr_default' => $default], ['addr_id' => $addr_id]);

            Db::commit();
            return json(['status' => 200, 'msg' => $this->lanMsg->doSuccess[$this->lanType]]);
        }catch (\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => $e->getMessage()]);
        }

    }
}